package com.css.fxfzypg.modules.baseData.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.baseData.entity.VillageTownCondition;
import com.css.fxfzypg.modules.baseData.entity.VillageTownHosPeoEntity;
import com.css.fxfzypg.modules.baseData.queryParams.VillageTownConditionQueryParams;
import com.css.fxfzypg.modules.baseData.service.VillageTownConditionService;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.util.ResponseCreator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @author Dbj
 * @date 2022年11月14日  10:09
 */
@RestController
@RequestMapping("/fxfzypg/baseData/villageTown")
@Slf4j
public class VillageTownConditionController {

    @Resource
    private VillageTownConditionService villageTownConditionService;
    /**
     * 全查询  模糊查询  地貌特性 气候特性为条件
     * @param request
     * @param queryParams
     * @return
     */
    @GetMapping("/getAllVillageTownCondition")
    public RestResponse getAllElevation(HttpServletRequest request, VillageTownConditionQueryParams queryParams){
        int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
        int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
        JSONObject allVillageTownCondition = villageTownConditionService.getAllVillageTownCondition(queryParams, curPage, pageSize);
        return ResponseCreator.createSuccessResponse(allVillageTownCondition);
    }

    /**
     * 新增elevation
     * @param villageTownCondition
     * @return
     */
    @PostMapping("/saveVillageTownCondition")
    public RestResponse saveVillageTownCondition (@RequestBody VillageTownCondition villageTownCondition) throws Exception {
        villageTownConditionService.saveVillageTownCondition(villageTownCondition);
        return ResponseCreator.createSuccessResponse("","保存成功");
    }

    /**
     * 根据id查询CountiesCondition数据
     * @param id
     * @return
     */
    @GetMapping("/getVillageTownCondition")
    public RestResponse getVillageTownCondition(@RequestParam("id") String id) {
        return ResponseCreator.createSuccessResponse(villageTownConditionService.getVillageTownConditionById(id));
    }

    /**
     * 逻辑删除，修改数据标志位
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public RestResponse delete(@RequestParam("id") String id) {
        villageTownConditionService.delete(id);
        return ResponseCreator.createSuccessResponse("","删除成功");
    }

    /**
     * 修改
     * @param villageTownCondition
     * @return
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody VillageTownCondition villageTownCondition)  {
        villageTownConditionService.update(villageTownCondition);
        return ResponseCreator.createSuccessResponse("","修改成功");
    }

    /**
     * 导入数据
     * @param file
     * @throws IOException
     */
    @PostMapping("/importExcelVillageTownCondition")
    public RestResponse importExcel(MultipartFile file) throws IOException {
        try {
            return villageTownConditionService.excelImport(file);
        } catch (Exception e) {
            return RestResponse.fail("导入数据失败");
        }

    }


    @PostMapping("/coverVillageTownCondition")
    public RestResponse cover(@RequestBody List<VillageTownCondition> list){
        RestResponse restResponse = null;
        try {
            villageTownConditionService.cover(list);
            restResponse=RestResponse.succeed("成功导入"+list.size()+"条.");
        } catch (Exception e) {
            log.error("覆盖乡镇概况失败",e);
            restResponse =   RestResponse.fail("覆盖乡镇概况失败");
        }
        return restResponse;
    }
}
